<?PHP

$password = $_POST['password'];
$repeat_password = $_POST['repeat_password'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$volunteer_position = $_POST['volunteer_position'];
$work_days = $_POST['work_days'];
$phone_number = $_POST['phone_number'];
$email = $_POST['email'];
$clothing_model = $_POST['clothing_model'];
if (!empty($_GET['flag'])) {
    $flag = $_GET['flag'];
} else {
    $flag = '';
}
if (!is_null($_POST['bank_account'])) {
    $bank_account = $_POST['bank_account'];
} else {
    $bank_account = '';
}

$mysqli = mysqli_init();

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2); //设置超时时间
$mysqli->real_connect('127.0.0.1', 'root', 'mysql');
$sql = "CREATE DATABASE IF NOT EXISTS CUPT DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
if ($mysqli->query($sql) == TRUE) {
   // printf("Table CUPT successfully created.\n");
}
mysqli_select_db($mysqli, "CUPT");


//插入数据 
if ($flag == 0) {
    $sql = "select * from volunteer where `email`='$email';";
    $rst = $mysqli->query($sql);
    if ($repeat_password !== $password) {
        echo "<br />两次输入密码不一致<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } elseif ($rst->num_rows != 0) {
        echo "<br />该邮箱已被注册，如想修改注册信息请点击“修改注册信息”<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } else {
        $sql = "INSERT INTO volunteer(password,name,sex,volunteer_position,work_days,phone_number,email,bank_account,clothing_model,secret_key) VALUES('$password','$name','$sex','$volunteer_position','$work_days','$phone_number','$email','$bank_account','$clothing_model','');";
        $mysqli->query($sql);
        if (mysqli_error($mysqli)) {
            echo "<br />数据库插入错误：" . mysqli_error($mysqli);
        } else {
        echo "<br />注册成功";
        mysqli_close($mysqli);
        }
    }
} elseif ($flag == 1) {
    if ($repeat_password !== $password) {
        echo "<br />两次输入密码不一致<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } else {
        $sql2 = "select * from volunteer where `email`='$email' and password='$password'";
        $rst2 = $mysqli->query($sql2);
        if ($rst2->num_rows == 0) {
            echo "<br />邮箱或密码错误，无法修改";
            echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
        } else {
            $sql = "UPDATE volunteer SET name ='$name',sex='$sex',volunteer_position='$volunteer_position',work_days='$work_days',phone_number='$phone_number',bank_account='$bank_account',clothing_model='$clothing_model' WHERE `email` = '$email' and `password`='$password';";
            $mysqli->query($sql);
            echo "<br />修改成功";
            mysqli_close($mysqli);
        }
    }
}
?>